const WXAPI = require('../../wxapi/main')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  
  updateUserInfo(e) {
    console.log(66666666);
    wx.getUserProfile({
      lang: 'zh_CN',
      desc: '用于完善会员资料',
      success: res => {
        this._updateUserInfo(res.userInfo)
        wx.setStorageSync('userInfo', res.userInfo)
      },
      fail: err => {
        wx.showToast({
          title: err.errMsg,
          icon: 'none'
        })
      }
    })
  },
  async _updateUserInfo(userInfo) {
    const postData = {
      token: wx.getStorageSync('token'),
      nick: userInfo.nickName,
      avatarUrl: userInfo.avatarUrl,
      city: userInfo.city,
      province: userInfo.province,
      gender: userInfo.gender,
    }
    const res = await WXAPI.modifyUserInfo(postData)
    console.log("ddddd:",res);
    if (res.code != 0) {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })
      return
    }
    wx.showToast({
      title: '登陆成功',
    })

    // wx.setStorageSync('token', res.data.token)
    // wx.setStorageSync('uid', res.data.uid)
    // 回到原来的地方放
    app.navigateToLogin = false
    wx.navigateBack();


  },

   getPhoneNumber: function(e) {
   console.log("dddd",e)
    if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
      wx.showModal({
        title: '提示',
        content: e.detail.errMsg,
        showCancel: false
      })
      return;
    }

    WXAPI.getMobilev2({
      token: wx.getStorageSync('token'),
      code: e.detail.code
    }).then(res => {
      app.navigateToLogin = false
      wx.navigateBack();
    })

   

    
    // this._getPhoneNumber(e)
  },

  _getPhoneNumber(e) {
    console.log(e);
    // let a =''
    // if(this.data.zcode){
    //   a =`https://wxjf.jpyyls.com:8086/prod-api/wx/miniapp/login/wx2480f54f3d6dbd22/`+this.data.zcode
    // }
    // else{
    //   a =`https://wxjf.jpyyls.com:8086/prod-api/wx/miniapp/login/wx2480f54f3d6dbd22/`+e.detail.code
    //   // this.setData({
    //   //   zcode:e.detail.code
    //   // })
    // }
   


    // if(this.data.scene != null){
    //   a= a+'/'+this.data.scene
    // }
    let that = this
    wx.request({
      url: a,
      method: 'POST',
      // data: {
      //   encryptedData:e.detail.encryptedData,
      //   iv:e.detail.iv,
      // },
      // header: header,
      success: function success(res) {
 
        if(res.data.code == 200){
          wx.setStorageSync('token','Bearer '+res.data.data);
          wx.redirectTo({
            url: '/pages/home/home',
          });
        }else{
          let cc= res.data.msg
          if(cc.indexOf('不存在') != -1){
            wx.setStorageSync('token','Bearer '+res.data.data);
            that.setData({
              dl:false
            })
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              iconduration: 5000,
            })
          }
          
        }
       
      },
      
    });



  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindGetUserInfo: function(e) {
    if (!e.detail.userInfo) {
      return;
    }
    if (app.globalData.isConnected) {
      wx.setStorageSync('userInfo', e.detail.userInfo)
      this.login();
    } else {
      wx.showToast({
        title: '当前无网络',
        icon: 'none',
      })
    }
  },
  login: function() {
    const that = this;
    const token = wx.getStorageSync('token');
    if (token) {
      WXAPI.checkToken(token).then(function(res) {
        if (res.code != 0) {
          wx.removeStorageSync('token')
          that.login();
        } else {
          // 回到原来的地方放
          app.navigateToLogin = false
          wx.navigateBack();
        }
      })
      return;
    }
    wx.login({
      
      success: function(res) {
        WXAPI.login(res.code).then(function(res) {
          console.log("999999:",res);
          if (res.code == 10000) {
            // 去注册
            that.registerUser();
            return;
          }
          if (res.code != 0) {
            // 登录错误
            wx.hideLoading();
            wx.showModal({
              title: '提示',
              content: '无法登录，请重试',
              showCancel: false
            })
            return;
          }
          wx.setStorageSync('token', res.data.token)
          wx.setStorageSync('uid', res.data.uid)
          // 回到原来的地方放
          app.navigateToLogin = false
          wx.navigateBack();
        })
      }
    })
  },
  registerUser: function() {
    let that = this;
    wx.login({
      success: function(res) {
        let code = res.code; // 微信登录接口返回的 code 参数，下面注册接口需要用到
        wx.getUserInfo({
          success: function(res) {
            let iv = res.iv;
            let encryptedData = res.encryptedData;
            let referrer = '' // 推荐人
            let referrer_storge = wx.getStorageSync('referrer');
            if (referrer_storge) {
              referrer = referrer_storge;
            }
            // 下面开始调用注册接口
            WXAPI.register( {
              code: code,
              encryptedData: encryptedData,
              iv: iv,
              referrer: referrer
            }).then(function(res) {
              wx.hideLoading();
              that.login();
            })
          }
        })
      }
    })
  }
})